<?php
require_once("functions.php");

function process( $sheetType, $file, $pickUpPath, $conversionID ) {
	global $basePath;

	switch( $sheetType ) {

		case 'Checklist-1_v1':
			require_once ($basePath . 'api/classes/gbif.custom-eml.php');
			require_once ($basePath . 'api/classes/gbif.checklist-1-1.php');
			$tplObj = new Checklist11($pickUpPath . $file);

			# Make EML
			$eml = new CustomEml();
			$sheet = $tplObj->getSheet(1);
			$eml->createEmlFromWorksheet( $sheet );
			$emlErrors = $eml->validate();
			$eml->generateXml(0);
			$xml = $eml->getRawXml();
			file_put_contents($pickUpPath . 'eml.xml', $xml );
			
			# Make Checklist
			$tplObj->validateChecklistData();
			$tplObj->writeChecklistData($pickUpPath);
			
			# Make metaxml
			file_put_contents($pickUpPath . '/meta.xml', $tplObj->meta->generateMetaData( $tplObj->getIndexes(), $tplObj->getFilenames() ) );
			
			$files = array_merge( array('eml.xml', 'meta.xml'), $tplObj->getFilenames() );
			$tplObj->zipArchive($pickUpPath . $conversionID . '.zip', $pickUpPath, $files );

			if ($emlErrors) {
				$errors->eml = $eml->getIssues();
			}
			return(array("pickupFile" => $pickUpPath . "/" . $conversionID . ".zip", "errors" => $errors));
			break;

		case 'Checklist-2_v1':
			require_once ($basePath . 'api/classes/gbif.custom-eml.php');
			require_once ($basePath . 'api/classes/gbif.checklist-2-1.php');
			$tplObj = new Checklist12($pickUpPath . $file);
			
			# Make EML
			$eml = new CustomEml();
			$sheet = $tplObj->getSheet(1);
			$eml->createEmlFromWorksheet( $sheet );
			$emlErrors = $eml->validate();
			$eml->generateXml(0);
			$xml = $eml->getRawXml();
			file_put_contents($pickUpPath . 'eml.xml', $xml );


			# Make Checklist
			$tplObj->buildLookUp();
			$tplObj->validateChecklistData();
			
			$tplObj->writeChecklistData($pickUpPath);
			
			# Make metaxml
			file_put_contents($pickUpPath . '/meta.xml', $tplObj->meta->generateMetaData( $tplObj->getIndexes(), $tplObj->getFilenames() ) );
			
			$files = array_merge( array('eml.xml', 'meta.xml'), $tplObj->getFilenames() );
			$tplObj->zipArchive($pickUpPath . $conversionID . '.zip', $pickUpPath, $files );

			if ($emlErrors) {
				$errors->eml = $eml->getIssues();
			}
			return(array("pickupFile" => $pickUpPath . "/" . $conversionID . ".zip", "errors" => $errors));
			break;

		case 'Checklist-3_v1':
			require_once ($basePath . 'api/classes/gbif.custom-eml.php');
			require_once ($basePath . 'api/classes/gbif.checklist-3-1.php');

			$tplObj = new Checklist13($pickUpPath . $file);
			
			# Make EML
			$eml = new CustomEml();
			$sheet = $tplObj->getSheet(1);
			$eml->createEmlFromWorksheet( $sheet );
			$emlErrors = $eml->validate();			
			$eml->generateXml(0);
			$xml = $eml->getRawXml();
			file_put_contents($pickUpPath . 'eml.xml', $xml );
			
			# Make Checklist
			$tplObj->validateChecklistData();
			$tplObj->writeChecklistData($pickUpPath);
			
			# Make metaxml
			file_put_contents($pickUpPath . 'meta.xml', $tplObj->meta->generateMetaData( $tplObj->getIndexes(), $tplObj->getFilenames() ) );
			
			$files = array_merge( array('eml.xml', 'meta.xml'), $tplObj->getFilenames() );
			$tplObj->zipArchive($pickUpPath . $conversionID . '.zip', $pickUpPath, $files );

			if ($emlErrors) {
				$errors->eml = $eml->getIssues();
			}
			return(array("pickupFile" => $pickUpPath . "/" . $conversionID . ".zip", "errors" => $errors));
			break;

		case 'Occurrence-1_v1':
			require_once ($basePath . 'api/classes/gbif.custom-eml.php');
			require_once ($basePath . 'api/classes/gbif.occurrence-1-1.php');

			$tplObj = new Occurrence1($pickUpPath . $file);
			
			# Make EML
			$eml = new CustomEml();
			$sheet = $tplObj->getSheet(1);

			$eml->createEmlFromWorksheet( $sheet, true );
			$emlErrors = $eml->validate();
			$eml->generateXml(0);
			$xml = $eml->getRawXml();
			file_put_contents($pickUpPath . 'eml.xml', $xml );
			
			# Make Checklist
			$tplObj->validateOccurrenceData();
			$tplObj->writeOccurrenceData($pickUpPath);
			
			# Make metaxml
			file_put_contents($pickUpPath . 'meta.xml', $tplObj->meta->generateMetaData( $tplObj->getIndexes(), $tplObj->getFilenames(), 'occurrence' ) );
			
			$files = array_merge( array('eml.xml', 'meta.xml'), $tplObj->getFilenames() );
			$tplObj->zipArchive($pickUpPath . $conversionID . '.zip', $pickUpPath, $files );

			if ($emlErrors) {
				$errors->eml = $eml->getIssues();
			}
			return(array("pickupFile" => $pickUpPath . "/" . $conversionID . ".zip", "errors" => $errors));
			break;

		case 'Occurrence-2_v1':
			require_once ($basePath . 'api/classes/gbif.custom-eml.php');
			require_once ($basePath . 'api/classes/gbif.occurrence-2-1.php');

			$tplObj = new Occurrence1($pickUpPath . $file);
			
			# Make EML

			$eml = new CustomEml();
			$sheet = $tplObj->getSheet(1);

			$eml->createEmlFromWorksheet( $sheet, true );
			$emlErrors = $eml->validate();
			$eml->generateXml(0);
			$xml = $eml->getRawXml();
			file_put_contents($pickUpPath . 'eml.xml', $xml );


			# Make Checklist
			$tplObj->buildLookUp();
			if(count($tplObj->arrayDiff)) {
				$warnings = 'Columns ' . implode(' ,',$tplObj->arrayDiff) . ' are not mapped in the Terms Sheet.';
			}

			$res = $tplObj->validateOccurrenceData();
			$tplObj->writeOccurrenceData($pickUpPath);
			
			# Make metaxml
			file_put_contents($pickUpPath . 'meta.xml', $tplObj->meta->generateOccurrenceMetaData() );

			$files = array_merge( array('eml.xml', 'meta.xml'), $tplObj->getFilenames() );
			$tplObj->zipArchive($pickUpPath . $conversionID . '.zip', $pickUpPath, $files );

			if ($emlErrors) {
				$errors->eml = $eml->getIssues();
			}
			return(array("pickupFile" => $pickUpPath . "/" . $conversionID . ".zip", "warnings" => $warnings));
			break;

		case 'Metadata-1_v1':
			require_once($basePath . 'api/classes/gbif.custom-eml.php');
			require_once($basePath . 'api/classes/gbif.general.php');

			$tplObj = new General($pickUpPath . $file);

			# Make EML
			$eml = new CustomEml();
			$sheet = $tplObj->getSheet(1);
			$eml->createEmlFromWorksheet( $sheet );
			$emlErrors = $eml->validate();			
			$eml->generateXml(0);
			$xml = $eml->getRawXml();
			file_put_contents($pickUpPath . 'eml.xml', $xml );
			
			if ($emlErrors) {
				$errors->eml = $eml->getIssues();
			}
			return(array("pickupFile" => $pickUpPath . 'eml.xml', "errors" => $errors));			
//			$files = array('eml.xml');
//			$tplObj->zipArchive($pickUpPath . $conversionID . '.zip', $pickUpPath, $files );
//			return($pickUpPath . "/" . $conversionID . ".zip");
			break;
			
		case 'Metadata-1_v0.9':
		case 'Occurrence-1_v0.9':
		case 'Checklist-3_v0.9':
		case 'Checklist-2_v0.9':
		case 'Checklist-1_v0.9':
			return(array("pickupFile" => "", "errors" => "Error: This template $sheetType has been depreciated and is no longer supported. Please download the lastest template. �We are sorry for the inconvenience."));
			break;
			
		default:
			return(array("pickupFile" => "", "errors" => "Error: Invalid Template: $sheetType"));
			break;
	}
							
	return( true );
}
?>